#include "message.hpp"

int Test1()
{
     yfrpc::RpcRequest::ptr rrp = yfrpc::MessageFactory::create<yfrpc::RpcRequest>();
    // yfrpc::BaseMessage::ptr bmp = yfrpc::MessageFactory::create(yfrpc::MType::REQ_RPC);
    // yfrpc::RpcRequest::ptr rrp1 = std::dynamic_pointer_cast<yfrpc::RpcRequest>(bmp);//父类转子类 需要强制转换
    Json::Value param;
    param["num1"] = 11;
    param["num2"] = 22;
    rrp->setMethod("Add");
    rrp->setParams(param);
    std::string str = rrp->serialize();
    std::cout << str << std::endl;

    yfrpc::BaseMessage::ptr bmp = yfrpc::MessageFactory::create(yfrpc::MType::REQ_RPC);
    bool ret = bmp->unserialize(str);
    if(ret == -1) return -1;
    ret = bmp->check();
    if(ret == false) return -1;
    yfrpc::RpcRequest::ptr rrp2 = std::dynamic_pointer_cast<yfrpc::RpcRequest>(bmp);
    std::cout << rrp2->method() << std::endl;
    std::cout << rrp2->params()["num1"].asInt() << std::endl;
    std::cout << rrp2->params()["num2"].asInt() << std::endl;
    return 1;
}

int Test2()
{
    yfrpc::TopicRequest::ptr trp = yfrpc::MessageFactory::create<yfrpc::TopicRequest>();
    trp->setTopicKey("news");
    trp->setOptype(yfrpc::TopicOptype::TOPIC_PUBLISH);
    trp->setTopicMsg("hello world");
    std::string str = trp->serialize();
    std::cout << str << std::endl;


    yfrpc::BaseMessage::ptr bmp = yfrpc::MessageFactory::create(yfrpc::MType::REQ_TOPIC);
    bool ret = bmp->unserialize(str);
    if(ret == -1) return -1;
    ret = bmp->check();
    if(ret == false) return -1;
    yfrpc::TopicRequest::ptr trp2 = std::dynamic_pointer_cast<yfrpc::TopicRequest>(bmp);
    std::cout << trp2->topicKey() << std::endl;
    std::cout << (int)trp2->optype() << std::endl;
    std::cout << trp2->topicMsg() << std::endl;
    return 1;
}

int Test3()
{
    yfrpc::ServiceRequest::ptr trp = yfrpc::MessageFactory::create<yfrpc::ServiceRequest>();    
    trp->setMethod("add");
    trp->setOptype(yfrpc::ServiceOptype::SERVICE_DISCOVERY);
    trp->setHost(yfrpc::Address("127.0.0.1", 9090));
    std::string str = trp->serialize();
    std::cout << str << std::endl;


    yfrpc::ServiceRequest::ptr trp2 = yfrpc::MessageFactory::create<yfrpc::ServiceRequest>();
    bool ret = trp2->unserialize(str);
    if(ret == false) return -1;
    ret = trp2->check();
    if(ret == false) return -1;
    std::cout << trp2->method() << std::endl;
    std::cout <<(int) trp2->optype() << std::endl;
    std::cout << trp2->host().first << std::endl;
    std::cout << trp2->host().second << std::endl;
    return 1;

}


int Test4()
{
    yfrpc::RpcResponse::ptr trp = yfrpc::MessageFactory::create<yfrpc::RpcResponse>();    
    trp->setRCode(yfrpc::RCode::RCODE_OK);
    Json::Value val;
    val = 33;
    trp->setResult(val);
    std::string str = trp->serialize();
    std::cout << str << std::endl;


    yfrpc::RpcResponse::ptr trp2 = yfrpc::MessageFactory::create<yfrpc::RpcResponse>();
    bool ret = trp2->unserialize(str);
    if(ret == false) return -1;
    ret = trp2->check();
    if(ret == false) return -1;
    std::cout << (int)trp2->rcode() << std::endl;
    std::cout << trp2->result().asInt()<< std::endl;
    return 1;
}


int Test5()
{
    yfrpc::TopicResponse::ptr trp = yfrpc::MessageFactory::create<yfrpc::TopicResponse>();    
    trp->setRCode(yfrpc::RCode::RCODE_OK);
    std::string str = trp->serialize();
    std::cout << str << std::endl;


    yfrpc::TopicResponse::ptr trp2 = yfrpc::MessageFactory::create<yfrpc::TopicResponse>();
    bool ret = trp2->unserialize(str);
    if(ret == false) return -1;
    ret = trp2->check();
    if(ret == false) return -1;
    std::cout << (int)trp2->rcode() << std::endl;
    return 1;
}

int Test6()
{
    yfrpc::ServiceResponse::ptr trp = yfrpc::MessageFactory::create<yfrpc::ServiceResponse>();    
    trp->setRCode(yfrpc::RCode::RCODE_OK);
    trp->setMethod("Add");
    trp->setOptype(yfrpc::ServiceOptype::SERVICE_DISCOVERY);
    std::vector<yfrpc::Address> addrs;
    addrs.push_back(yfrpc::Address("127.0.0.1", 9090));
    addrs.push_back(yfrpc::Address("127.0.0.2", 9091));
    trp->setHost(addrs);
    
    std::string str = trp->serialize();
    std::cout << str << std::endl;


    yfrpc::ServiceResponse::ptr trp2 = yfrpc::MessageFactory::create<yfrpc::ServiceResponse>();
    bool ret = trp2->unserialize(str);
    if(ret == false) return -1;
    ret = trp2->check();
    if(ret == false) return -1;
    std::cout << (int)trp2->rcode() << std::endl;
    std::cout << (int)trp2->optype()<< std::endl;
    std::cout << trp2->method()<< std::endl;

    std::vector<yfrpc::Address> addrs2 = trp2->hosts();
    for(yfrpc::Address addr : addrs2)
    {
        std::cout << addr.first << ":" << addr.second<< std::endl;
    }
    return 1;
}

int main()
{
    //Test1();
    //Test2();
    //Test3();
    //Test4();
    //Test5();
    Test6();
    return 0;
}




